package com.h2.ref.server.web.captcha;

import java.awt.Color;
import java.util.Random;

import nl.captcha.Captcha;
import nl.captcha.backgrounds.FlatColorBackgroundProducer;
import nl.captcha.text.producer.DefaultTextProducer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.h2.ref.server.util.ImageUtils;
import com.h2.ref.server.web.captcha.dao.CaptchaDaoInMem;

public class CaptchaService {
   
   private static final Logger LOG =
      LoggerFactory.getLogger(CaptchaService.class);
   
   ////
   
   private String _fileName = "captcha_";
   
   private String _filePath;
   
   private Integer _imageHeight;
   
   private Integer _imageWidth;
   
   private Integer _solutionSize;
   
   ////
   
   private CaptchaDaoInMem _captchaDao;
   
   private static final Random RAN_GENERATOR = new Random();

   ////
   ////
   
   public CaptchaVO generate() {
      Captcha simpleCaptcha = createCaptcha();
      
      int randNumber = RAN_GENERATOR.nextInt();
      String uid = getFileName() + randNumber + ".png";
      ImageUtils.writeImage(
               uid, getFilePath(),
               simpleCaptcha.getImage());
      return getCaptchaDao().createSolution(uid, simpleCaptcha.getAnswer());
   }
   
   public Boolean validate(Integer captchaId, String solution) {
      
      //always remove CAPTCHA when tested
      CaptchaVO captchaVO = getCaptchaDao().removeCaptcha(captchaId);
      
      if (null == captchaVO) {
         LOG.debug("CAPTCHA ID is invalid.");
      }
      else if (!captchaVO.getSolution().equalsIgnoreCase(solution)) {
         LOG.debug("The CAPTCHA solution is invalid.");
      }
      return Boolean.TRUE;
   }
   
   protected Captcha createCaptcha() {
      Captcha simpleCaptcha = new Captcha.Builder(
            getImageWidth(), getImageHeight())
      .addText(new DefaultTextProducer(getSolutionSize()))
      .addBackground(new FlatColorBackgroundProducer(Color.WHITE))
      .addNoise()
      .build();
      
      return simpleCaptcha;
   }

   /**
    * @return the fileName
    */
   public String getFileName() {
      return _fileName;
   }

   /**
    * @param fileName the fileName to set
    */
   public void setFileName(String fileName) {
      _fileName = fileName;
   }

   /**
    * @return the imageHeight
    */
   public Integer getImageHeight() {
      return _imageHeight;
   }

   /**
    * @param imageHeight the imageHeight to set
    */
   public void setImageHeight(Integer imageHeight) {
      _imageHeight = imageHeight;
   }

   /**
    * @return the imageWidth
    */
   public Integer getImageWidth() {
      return _imageWidth;
   }

   /**
    * @param imageWidth the imageWidth to set
    */
   public void setImageWidth(Integer imageWidth) {
      _imageWidth = imageWidth;
   }

   /**
    * @return the solutionSize
    */
   public Integer getSolutionSize() {
      return _solutionSize;
   }

   /**
    * @param solutionSize the solutionSize to set
    */
   public void setSolutionSize(Integer solutionSize) {
      _solutionSize = solutionSize;
   }

   /**
    * @return the captchaDao
    */
   public CaptchaDaoInMem getCaptchaDao() {
      return _captchaDao;
   }

   /**
    * @param captchaDao the captchaDao to set
    */
   public void setCaptchaDao(CaptchaDaoInMem captchaDao) {
      _captchaDao = captchaDao;
   }

   /**
    * @return the filePath
    */
   public String getFilePath() {
      return _filePath;
   }

   /**
    * @param filePath the filePath to set
    */
   public void setFilePath(String filePath) {
      _filePath = filePath;
   }
}
